/*
 * Copyright 2011-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.dev.grpc.demo.acl.service;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.grpc.stub.StreamObserver;
import me.ijleex.dev.grpc.demo.acl.ACLProto;
import me.ijleex.dev.grpc.demo.acl.ACLServiceGrpc;

/**
 * ACLService.
 *
 * @author liym
 * @since 2022-08-10 22:17 新建
 */
public class ACLService extends ACLServiceGrpc.ACLServiceImplBase {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void login(ACLProto.LoginRequest request, StreamObserver<ACLProto.LoginResponse> responseObserver) {
        int metaCount = request.getMetaCount();
        Map<String, String> metaMap = request.getMetaMap();
        String bearerToken = request.getBearerToken();
        try {
            ACLProto.LoginToken token = ACLProto.LoginToken.newBuilder()
                    .setAccessorId("ijliym")
                    .setSecretId("123456")
                    .build();
            ACLProto.LoginResponse response = ACLProto.LoginResponse.newBuilder()
                    .setToken(token)
                    .build();
            responseObserver.onNext(response);
        } catch (Exception e) {
            logger.error("login error", e);
            responseObserver.onError(e);
        }
        responseObserver.onCompleted();
    }

    @Override
    public void logout(ACLProto.LogoutRequest request, StreamObserver<ACLProto.LogoutResponse> responseObserver) {
        String token = request.getToken();
        ACLProto.LogoutResponse response = ACLProto.LogoutResponse.newBuilder()
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

}
